<?php


namespace app\model;


use think\Model;

class Menu extends Model
{
    //主键
    protected $pk = "mid";
    //表名
    protected $name = "menu";
    // 设置当前模型对应的完整数据表名称
    protected $table = 'jw_menu';

    protected $schema = [
        "sid" => "int",//'上级ID 第一级0',
        "name" => "string",//'名称',
        "icon" => "string",//'图标',
        "sort" => "int",//'排序',
        "type" => "int",// '类型 1菜单 2api 3按钮',
        "site" => "string",// '链接地址',
        "addtime" => "datetime",// '添加时间',
        "lasttime" => "datetime",// '更新时间',
    ];

    // 获取所有菜单
    public static function menuList($sid = 0)
    {
        $data = self::where('sid', $sid)->field("mid,sid,name,icon,sort,type,site")->order('sort','ase')->select()->toArray();
        foreach ($data as $key => $val) {
            $data[$key]["children"] = self::menuList($val['mid']);
            if(empty($data[$key]["children"])){
                unset($data[$key]["children"]);
            }
        }
        return $data;
    }

    /**
     * info: 权限节点
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/11 9:55
     * Url: menuListFinld
     */
    public static function menuListFinld($sid = 0)
    {
        $data = self::where('sid', $sid)->field("mid,name")->order('sort','ase')->select()->toArray();
        foreach ($data as $key => $val) {
            $data[$key]["children"] = self::menuListFinld($val['mid']);
            if(empty($data[$key]["children"])){
                unset($data[$key]["children"]);
            }
        }
        return $data;
    }

    //根据角色获取菜单
    public static function getUserMenus($sidList, $sid = 0)
    {
        $list = self::where('mid', 'in', $sidList)->where(['sid' => $sid, 'type' => 1])->order('sid', 'ase')->select()->toArray();
        foreach ($list as $key => $val) {
            $list[$key]["children"] = self::getUserMenus($sidList, $val['mid']);
            if(empty($list[$key]["children"])){
                unset($list[$key]["children"]);
            }
        }
        return $list;
    }

    // 获取用户菜单 list              权限ID列表
    public static function getAutoApi($sidList)
    {
        $list = self::where('mid', 'in', $sidList)->withAttr('site', function ($value, $data) {
        return strtolower($value);
    })
            ->where(['type' => [2, 3]])
            ->order('sid', 'ase')
            ->cache(10)
            ->field("mid,site")->select()->toArray();
        return $list;
    }

    //查父级
    public static function getFather($mid,$arr=[]){
        array_unshift($arr,$mid);
        $data = self::where('mid',$mid)->field('sid')->find();
        if ($data->sid){
            return self::getFather($data->sid,$arr);
        }
        return $arr;
    }

    //查子级
    public static function getSon($mid){
        $data = self::where('sid', $mid)->field("mid")->select()->toArray();
        $data = array_column($data,'mid');
        foreach ($data as $val) {
            $dataList = self::getSon($val);
            if(!empty($data)){
                $data = array_merge($data,$dataList);
            }
        }
        return $data;
    }
}